package com.nba.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nba.bean.Hotspot;
import com.nba.bean.Pdate;
import com.nba.service.HotService;
import com.nba.service.PdateService;
import com.nba.service.PlayerService;
import com.nba.service.TeamService;
import com.nba.service.impl.HotServiceImpl;
import com.nba.service.impl.PdateServiceImpl;
import com.nba.service.impl.PlayerServiceImpl;
import com.nba.service.impl.TeamServiceImpl;

@WebServlet("/hotServlet.do")
public class HotServlet extends HttpServlet{
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String opt = request.getParameter("opt");
		switch(opt) {
			case "0":
				delete(request,response);
				break;
			case "1":
				add(request,response);
				break;
			case "2":
				deleteAll(request,response);
				break;
			case "3":
				update(request,response);
				break;
			case "4":
				break;
		}
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HotService hs =  new HotServiceImpl();
		int hotspotId = Integer.parseInt(request.getParameter("hotspotId"));
		hs.deleteHot(hotspotId);
		response.sendRedirect("SearchServlet.do");
	}
	public void deleteAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HotService hs =  new HotServiceImpl();
		String ids = request.getParameter("ids");
		if(ids!=null) {
			String[] idArray = ids.split("_");
			for (int i = 0; i < idArray.length; i++) {
				hs.deleteHot(Integer.parseInt(idArray[i]));
			}
		}
	}
	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println(req.getParameter("hotspotId")+":"+req.getParameter("seasonId"));
		int hotspotId = Integer.parseInt(req.getParameter("hotspotId"));
		int seasonId = Integer.parseInt(req.getParameter("seasonId"));
		double hotspotOne = Double.parseDouble(req.getParameter("hotspotOne"));
		double hotspotTwo = Double.parseDouble(req.getParameter("hotspotTwo"));
		double hotspotThree = Double.parseDouble(req.getParameter("hotspotThree"));
		double hotspotFour = Double.parseDouble(req.getParameter("hotspotFour"));
		double hotspotFive = Double.parseDouble(req.getParameter("hotspotFive"));
		double hotspotSix = Double.parseDouble(req.getParameter("hotspotSix"));
		double hotspotSeven = Double.parseDouble(req.getParameter("hotspotSeven"));
		double hotspotEight = Double.parseDouble(req.getParameter("hotspotEight"));
		double hotspotNine = Double.parseDouble(req.getParameter("hotspotNine"));
		double hotspotTen = Double.parseDouble(req.getParameter("hotspotTen"));
		double hotspotEleven = Double.parseDouble(req.getParameter("hotspotEleven"));
		double hotspotTwelve = Double.parseDouble(req.getParameter("hotspotTwelve"));
		double hotspotThirteen = Double.parseDouble(req.getParameter("hotspotThirteen"));
		double hotspotFourteen = Double.parseDouble(req.getParameter("hotspotFourteen"));
		int hotspotDate = Integer.parseInt(req.getParameter("hotspotDate"));
		String playerName = req.getParameter("playerName");
		PlayerService ps = new PlayerServiceImpl();
		int playerId = ps.selectPlayerByName(playerName).getPlayerId();
			
		Hotspot hotspot = new Hotspot(hotspotId,playerId, hotspotOne, hotspotTwo, hotspotThree, hotspotFour, hotspotFive, hotspotSix, hotspotSeven, hotspotEight, hotspotNine, hotspotTen, hotspotEleven, hotspotTwelve, hotspotThirteen, hotspotFourteen, seasonId, hotspotDate);
		HotService hs = new HotServiceImpl();
		hs.updateHot(hotspot);
	
		resp.sendRedirect("SearchServlet.do");
	}

	public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		int seasonId = Integer.parseInt(req.getParameter("seasonId"));
		double hotspotOne = Double.parseDouble(req.getParameter("hotspotOne"));
		double hotspotTwo = Double.parseDouble(req.getParameter("hotspotTwo"));
		double hotspotThree = Double.parseDouble(req.getParameter("hotspotThree"));
		double hotspotFour = Double.parseDouble(req.getParameter("hotspotFour"));
		double hotspotFive = Double.parseDouble(req.getParameter("hotspotFive"));
		double hotspotSix = Double.parseDouble(req.getParameter("hotspotSix"));
		double hotspotSeven = Double.parseDouble(req.getParameter("hotspotSeven"));
		double hotspotEight = Double.parseDouble(req.getParameter("hotspotEight"));
		double hotspotNine = Double.parseDouble(req.getParameter("hotspotNine"));
		double hotspotTen = Double.parseDouble(req.getParameter("hotspotTen"));
		double hotspotEleven = Double.parseDouble(req.getParameter("hotspotEleven"));
		double hotspotTwelve = Double.parseDouble(req.getParameter("hotspotTwelve"));
		double hotspotThirteen = Double.parseDouble(req.getParameter("hotspotThirteen"));
		double hotspotFourteen = Double.parseDouble(req.getParameter("hotspotFourteen"));
		int hotspotDate = Integer.parseInt(req.getParameter("hotspotDate"));
		String playerName = req.getParameter("playerName");
		PlayerService ps = new PlayerServiceImpl();
		int playerId = ps.selectPlayerByName(playerName).getPlayerId();
			
		Hotspot hotspot = new Hotspot(playerId, hotspotOne, hotspotTwo, hotspotThree, hotspotFour, hotspotFive, hotspotSix, hotspotSeven, hotspotEight, hotspotNine, hotspotTen, hotspotEleven, hotspotTwelve, hotspotThirteen, hotspotFourteen, seasonId, hotspotDate);
		HotService hs = new HotServiceImpl();
		hs.addHot(hotspot);
		
		resp.sendRedirect("SearchServlet.do");
	}
}

